<?php
//这个控制器在前台调用 所以继承controller
//if(!defined('IS_TASK_IN')) exit;
class archtmlController extends accessController{
	public function __construct(){
		parent::__construct();
		$this->svc=load_service('archtml',P_SVC);
        
        $this->url_service=load_service('url',P_SVC);
        $this->url_service->set_module_id(11);
		$this->pagesize=$this->url_service->pagesize();
		
		//开关
		//时间
		
		$this->task_tag_index =1;
		$this->task_time_index=3600;
		
		$this->task_tag_list  =1;
		$this->task_time_list =3600;
		
		$this->task_tag_show  =1;
		//$this->task_time_show =3600;
		
		$this->table1=table('arc');
		$this->table3=table('category');
	}
	public function s(){
		//return array('arc','attrtaglist','wordtaglist');
		return array();
	}
	//生成首页
	//$cms_task = "html=tohtml_index";
	public function tohtml_index(){
		if(!$this->task_tag_index) return false;
		list($http,$file)=$this->url_service->index();
		if(time() - filemtime($file) > $this->task_time_index){
			$this->svc->_index();
		}
		return true;
	}
	//生成列表页
	//$cms_task = "html=tohtml_list&catid=$catid&page_id=$page_id";
	public function tohtml_list(){
		if(!$this->task_tag_list) return false;
		$catid=intval($_GET['catid']);
		if($catid==0) return false;
		$page_id=intval($_GET['page_id']);
		$page_id=max($page_id,1);
		
		$mytime=time();
		
		$query="select `id` from `{$this->table3}` where `id`=:id limit 1";
		$CAT=$this->m->select($query,array('id'=>$catid));
		if(!is_array($CAT)) return false;
		
		$query="select count(*) from `{$this->table1}` where `status`='1' and `arccat_id`=:id";
		$_c=$this->m->get_field($query,array('id'=>$catid));
		
		$num=1;
		$totalpage=max(ceil(intval($_c)/$this->pagesize),1);
		
		//list($http_original,$file_original)=$this->svc->_get_rule('list_i');
		
		$fid = $page_id;
		//$http=str_replace(array('{arccat_id}','{i}'),array($catid,$fid),$http_original);
		//$file=str_replace(array('{arccat_id}','{i}'),array($catid,$fid),$file_original);
        list($http,$file)=$this->url_service->var_list_i($catid,$fid)->list_i();
		
		if($fid >= 1 && $fid <= $totalpage && $mytime - filemtime($file) > $this->task_tag_list){
			$this->svc->_list($catid,$page_id,$this->pagesize,$fid,$num);
		}
		
		$fid = $page_id + 1;
		//$http=str_replace(array('{arccat_id}','{i}'),array($catid,$fid),$http_original);
		//$file=str_replace(array('{arccat_id}','{i}'),array($catid,$fid),$file_original);
        list($http,$file)=$this->url_service->var_list_i($catid,$fid)->list_i();
		
		if($fid >= 1 && $fid <= $totalpage && $mytime - filemtime($file) > $this->task_tag_list){
			$this->svc->_list($catid,$page_id,$this->pagesize,$fid,$num);
		}
		
		$fid = $totalpage + 1 - $page_id;
		//$http=str_replace(array('{arccat_id}','{i}'),array($catid,$fid),$http_original);
		//$file=str_replace(array('{arccat_id}','{i}'),array($catid,$fid),$file_original);
        list($http,$file)=$this->url_service->var_list_i($catid,$fid)->list_i();
		
		if($fid >= 1 && $fid <= $totalpage && $mytime - filemtime($file) > $this->task_tag_list){
			$this->svc->_list($catid,$page_id,$this->pagesize,$fid,$num);
		}
		
		return true;
	}
	//生成内容页
	//$cms_task = "html=tohtml_show&id=$id";
	public function tohtml_show(){
		if(!$this->task_tag_show) return false;
		
		$id=intval($_GET['id']);
		if($id==0) return false;
		
		//list($http,$file)=$this->svc->_get_rule('show');
		
		$query="select `id`,`add_time`,`edit_time`,`status` from `{$this->table1}` where `id`=:id limit 1";
		$item=$this->m->select($query,array('id'=>$id));
		if(!is_array($item) || $item['status'] == 0) return false;
		
		////////////////////////////////////////////////////////////////////
		$c=date('Ym',$item['add_time']);
		//$http=str_replace(array('{cat}','{id}'),array($c,$id),$http);
		//$file=str_replace(array('{cat}','{id}'),array($c,$id),$file);
        list($http,$file)=$this->url_service->var_show($c,$id)->show();
		
		if($item['edit_time'] > filemtime($file)){
			$this->svc->_show($id);
		}
		return true;
	}
	
//
}